function LayerManager(config, map) {
    this._init_(config, map);
}
LayerManager.prototype = {
    constructor: LayerManager,
    _init_: function (config, map) {
        if (config != null && config != undefined) {
            this.config = config;
            this.layersarr = this.initLayerArr(config);
        }
        if (map != null && map != undefined) {
            this.map = map;
        }
        this.layerCreator = new LayerCreator();

    },
    setCfg: function (config, map) {
        this.config = config;
        this.layersarr = this.initLayerArr(config);
        this.map = map;
    },
    initLayerArr: function (layersConfig, hideMap) {
        if (!layersConfig || layersConfig.length == 0) return null;

        var obj = {};

        for (var i = 0; i < layersConfig.length; i++) {
            var config = layersConfig[i];

            if (hideMap && hideMap[config.groupId]) continue;

            $.extend(obj, this.getConfig(config, hideMap));
        }

        var arr_ = [];
        for (var sn in obj) {
            arr_[sn] = obj[sn];
        }

        var arr = [];
        for (var i = 0; i < arr_.length; i++) {
            if (arr_[i] == undefined) continue;
            arr = arr.concat(arr_[i]);
        }

        if (arr.length == 0) return null;
        else return arr;
    },
    getConfig: function (config, hideMap) {
        var obj = {};

        if (config.layers) {
            for (var i = 0; i < config.layers.length; i++) {
                var cfg_ = config.layers[i];

                if (hideMap && hideMap[cfg_.bklayerId]) continue;//hideMap中的图层，跳过不加载

                obj[cfg_.bklayerSn] = cfg_;
            }
        }

        if (config.children) {
            for (var i = 0; i < config.children.length; i++) {
                $.extend(obj, this.getConfig(config.children[i], hideMap));
            }
        }

        return obj;
    },
    initLayer: function () {
        if (this.layersarr == null) {
            return;
        }
        var layers = [];
        for (var i = 0; i < this.layersarr.length; i++) {
            var lcfg = this.layersarr[i];
            try {
                layers.push(this.layerCreator.createLayer(lcfg));
            } catch (e) {
                console.log("加载图层失败");
                console.log(lcfg);
            }
        }
        if (layers.length > 0) {
            this.map.setLayers(layers);
        }
    },
    addLayers: function (layers) {
        let layersarr = this.initLayerArr(layers);
        for (var i = 0; i < layersarr.length; i++) {
            var lcfg = layersarr[i];
            try {
                this.map.addLayer(this.layerCreator.createLayer(lcfg));
            } catch (e) {
                console.log("加载图层失败");
                console.log(lcfg);
            }
        }
    },
    addLayer: function (lcfg) {
        try {
            this.map.addLayer(this.layerCreator.createLayer(lcfg));
        } catch (e) {
            console.log("加载图层失败");
        }
    },
    /**
     *
     * 获取地图所有图层id
     */
    getAllLayer: function (isId=true) {
      const layersIds = []
      const allLayer = this.map.getLayers().array_;
      for (var i = allLayer.length; --i >= 0;) {
        if(!allLayer[i]){
          continue
        }
        if(isId){
          layersIds.push(allLayer[i].get("bklayerId"))
        }else{
          layersIds.push(allLayer[i].values_)
        }
      };
      return layersIds
    },
    /**
     * 控制图层显示或隐藏,可一次控制多个图层
     * @param params object {String layerId:Boolean showOrHidden}  例如 {'aaa':ture,'bbb':false}
     */
    layerContrl: function (params) {
        var truth = null;
        var allLayer = this.map.getLayers().array_;
        for (var i = allLayer.length; --i >= 0;) {
            truth = params[allLayer[i].get("bklayerId")];
            if (truth != undefined) { allLayer[i].setVisible(truth); };
        };
    }
}
